import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_rfid_dmo/rfid/rfid_connection.dart';
import '../rfid/model/rfid_model.dart';

class BarcodePage extends StatefulWidget {
  const BarcodePage({Key? key}) : super(key: key);

  @override
  State<BarcodePage> createState() => _BarcodePageState();
}

class _BarcodePageState extends State<BarcodePage> {
  final RFIDConnection _rfidService = RFIDConnection();
  // 示例数据
  List<String> barcodeList = [];

  // 模拟扫码
  void _onScan() {
    _rfidService.readQRCode();
  }

  StreamSubscription<RfidResult>? msgSub;
  @override
  void initState() {
    super.initState();
    msgSub = _rfidService.resultStream.listen((result) {
      switch (result.type) {
        case RfidResultType.qrCode:
          // 处理二维码等
          if(result.data != null){
            barcodeList.insert(0, result.data);
            setState(() {
            });
          }
          break;
        default:
          break;
      }
    });
  }

  @override
  void dispose() {
    msgSub?.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          // 顶部表头
          Container(
            color: Colors.blue[700],
            height: 40,
            child: const Row(
              children: [
                SizedBox(
                  width: 60,
                  child: Center(
                    child: Text(
                      '序号',
                      style: TextStyle(color: Colors.white, fontSize: 16),
                    ),
                  ),
                ),
                Expanded(
                  child: Center(
                    child: Text(
                      '二维码/条形码',
                      style: TextStyle(color: Colors.white, fontSize: 16),
                    ),
                  ),
                ),
              ],
            ),
          ),
          if (barcodeList.isEmpty)
            const Center(
              child: Text('请点击 读取二维码按钮 进行操作'),
            ),
          // 列表
          Expanded(
            child: ListView.builder(
              itemCount: barcodeList.length,
              itemBuilder: (context, index) {
                return Container(
                  decoration: const BoxDecoration(
                    border: Border(
                      bottom: BorderSide(color: Color(0xFFE0E0E0), width: 1),
                    ),
                  ),
                  child: Row(
                    children: [
                      SizedBox(
                        width: 60,
                        child: Center(
                          child: Text(
                            '${barcodeList.length - 1 - index}',
                            style: const TextStyle(fontSize: 20),
                          ),
                        ),
                      ),
                      Expanded(
                        child: Padding(
                          padding: const EdgeInsets.symmetric(vertical: 12.0),
                          child: Text(
                            barcodeList[index],
                            style: const TextStyle(fontSize: 20),
                          ),
                        ),
                      ),
                    ],
                  ),
                );
              },
            ),
          ),
          // 底部按钮
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: SizedBox(
              width: double.infinity,
              height: 56,
              child: OutlinedButton(
                style: OutlinedButton.styleFrom(
                  side: const BorderSide(color: Colors.cyan, width: 1.5),
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(12),
                  ),
                  backgroundColor: Colors.white,
                ),
                onPressed: _onScan,
                child: const Text(
                  '读取 二维码/条形码',
                  style: TextStyle(fontSize: 22, color: Colors.black87),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
